package zhuyun.code.photoviews.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import zhuyun.code.photoviews.R

/**
 * @author: Administrator
 * @date: 2021/12/17
 */
class PhotoViewAdapter(private val context: Context, private val mCurrentSelectPosition:Array<Int>):BaseAdapter() {
    private var inflater: LayoutInflater = LayoutInflater.from(context)
    companion object{
        const val N = 10
    }
    fun setCurrentSelectPosition(position: Int){
        this.mCurrentSelectPosition[0] = position
        notifyDataSetChanged()
    }
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var itemView:View
        var holder: ViewHolder
        if(convertView == null){
            itemView = inflater.inflate(R.layout.item_photo_list_view,null)
            holder = ViewHolder()
            holder.imageView = itemView.findViewById(R.id.imageView)
            holder.imageIndex = itemView.findViewById(R.id.index)
            itemView.tag = holder

        } else {
            itemView = convertView
            holder = convertView.tag as ViewHolder
        }
        holder.imageIndex?.text = "${toRealPosition(position)+1}"
        if(mCurrentSelectPosition[0] != -1){
            holder.imageView?.isSelected = position == mCurrentSelectPosition[0]
            holder.imageView?.isPressed = position == mCurrentSelectPosition[0]
        }
        itemView.setOnClickListener{
            setCurrentSelectPosition(position)
        }
        return itemView
    }

    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return 18
    }
    class ViewHolder{
        var imageView:ImageView? = null
        var imageIndex: TextView? = null
    }

    private fun toRealPosition(position: Int):Int{
        return (position-4+ N)% N
    }

}